﻿using Dapper;
using Dapper.Contrib.Extensions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoPrint.BJPT.DAL
{
    public class BaseDAL<T> where T : class, new()
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        protected readonly string CONNECTSTRING = ConfigurationManager.ConnectionStrings["Sqlite"].ToString();

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public List<T> Query(string sql)
        {
            using (IDbConnection con = new SQLiteConnection(CONNECTSTRING))
            {
                var list = con.Query<T>(sql).ToList();
                return list;
            }
        }

        /// <summary>
        /// 参数化查询[Param]
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="Param">参数值</param>
        /// <returns></returns>
        public List<T> Query(string sql, string Param)
        {
            using (IDbConnection con = new SQLiteConnection(CONNECTSTRING))
            {
                var list = con.Query<T>(sql, new { Param }).ToList();
                return list;
            }
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public long Insert(T entity)
        {
            using (IDbConnection con = new SQLiteConnection(CONNECTSTRING))
            {
                var rs = con.Insert(entity);
                return rs;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            using (IDbConnection con = new SQLiteConnection(CONNECTSTRING))
            {
                var rs = con.Update(entity);
                return rs;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            using (IDbConnection con = new SQLiteConnection(CONNECTSTRING))
            {
                var rs = con.Delete(entity);
                return rs;
            }
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int Excute(string sql)
        {
            using (IDbConnection con = new SQLiteConnection(CONNECTSTRING))
            {
                var rs = con.Execute(sql);
                return rs;
            }
        }

        /// <summary>
        /// 执行参数化sql语句[占位符Param]
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="Param">参数</param>
        /// <returns></returns>
        public int Excute(string sql, string Param)
        {
            using (IDbConnection con = new SQLiteConnection(CONNECTSTRING))
            {
                var rs = con.Execute(sql, new { Param });
                return rs;
            }
        }

        /// <summary>
        /// 执行参数化sql where in语句[占位符Params]
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="Params">参数数组</param>
        /// <returns></returns>
        public int Excute(string sql, string[] Params)
        {
            using (IDbConnection con = new SQLiteConnection(CONNECTSTRING))
            {
                var rs = con.Execute(sql, new { Params });
                return rs;
            }
        }
    }
}
